﻿using System.Text;

namespace Succell.Dnn.JQuery
{
    /// <summary>
    /// Defines a client event handler function.
    /// </summary>
    public class ClientEventHandler
    {
        #region properties

        private string Signature { get; set; }

        /// <summary>
        /// Gets the function body of the client event handler.
        /// </summary>
        public string FunctionBody { get; private set; }

        #endregion

        #region constructors

        /// <summary>
        /// Creates a <see cref="ClientEventHandler"/> with the given signature and function body.
        /// </summary>
        /// <param name="signature">The signature for the client event handler.</param>
        /// <param name="functionBody">The function body for the client event handler.</param>
        public ClientEventHandler(string signature, string functionBody)
        {
            this.Signature = signature;
            this.FunctionBody = functionBody;
        }

        /// <summary>
        /// Creates a new <see cref="ClientEventHandler"/> with function code prepended to an
        /// existing <see cref="ClientEventHandler"/>.
        /// </summary>
        /// <param name="baseHandler">The existing base <see cref="ClientEventHandler"/>.</param>
        /// <param name="prependBody">The code to prepend to the function body.</param>
        public ClientEventHandler(ClientEventHandler baseHandler, string prependBody)
        {
            this.Signature = baseHandler.Signature;
            this.FunctionBody = prependBody + baseHandler.FunctionBody;
        }

        #endregion

        #region methods

        /// <summary>
        /// Gets the string representation of this <see cref="ClientEventHandler"/>.
        /// </summary>
        /// <returns>The string representation of this <see cref="ClientEventHandler"/>.</returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("function(");
            sb.Append(this.Signature);
            sb.Append("){");
            sb.Append(this.FunctionBody);
            sb.Append("}");
            return sb.ToString();
        }

        #endregion
    }
}
